from tkinter import *  
from tkinter import messagebox
from tkinter import filedialog
from config.Instrument import Instrument_img  
from dao.Result import Result
from PIL import ImageTk
from middleWare import Middleware

class EnrollWindow:  
    def __init__(self, master):  
        self.imgbyte = open("static/默认头像.png", "rb").read()
        self.master = master  
        self.master.title("管理员注册界面")  
        self.master.resizable(0, 0)  
        self.master.iconbitmap("static\\favicon.ico")  

        #设置背景色
        self.master.configure(background="#ffffff")

        self.width = self.master.winfo_screenwidth()  
        self.height = self.master.winfo_screenheight()  
  
        self.x = (self.width - 600) // 2  
        self.y = (self.height - 400) // 2   
  
        self.master.geometry("{}x{}+{}+{}".format(600, 400, self.x, self.y))  
  
        self.user = Label(self.master, text="账号")  
        #设置背景色
        self.user.config(background="#ffffff")
        self.user.place(x=270, y=70)  
        self.password = Label(self.master, text="密码") 
        #设置背景色
        self.password.config(background="#ffffff") 
        self.password.place(x=270, y=130)  
        self.txt_user = Entry(self.master, width=30)  
        #设置背景色
        self.txt_user.config(background="#ffffff")
        self.txt_user.place(x=320, y=70)  
        self.txt_password = Entry(self.master, width=30)  
        #设置背景色
        self.txt_password.config(background="#ffffff")
        self.txt_password.place(x=320, y=130)  
        self.txt_password.config(show="*")  
        self.txt_user.focus()  
        self.txt_user.bind("<Return>", lambda e: self.txt_password.focus())  

        #设置一个文本域，文本内容为 ” “
        self.text_head = Label(self.master, text="头像")
        self.text_head.config(background="#ffffff")
        self.text_head.place(x=270, y=200)

        #设置一个区域显示图片
        self.img = PhotoImage(file='static/默认头像.png')
        self.btn1 = Button(self.master,image=self.img, width=40, height=40,relief=FLAT,padx=3,pady=3,background="#ffffff")
        self.btn1.place(x=320, y=190)
        self.btn1.config(command=self.changehead)
        

        self.btn = Button(self.master, text="注册",width=10, command=self.clicked)  
        #设置按钮样式
        self.btn.config(width=30,background="#3b80f9",fg='#ffffff',relief='flat',activebackground='#3b80f9',activeforeground='#3b80f9')
        self.btn.place(x=310, y=270)  


        #读取图片
        self.photo = PhotoImage(file='static/跑步.png')
        #创建一个区域显示图片
        self.line = Label(self.master, image=self.photo, bg='#ffffff')
        self.line.config(width=250, height=400, bg='#fafafa')
        self.line.place(x=0, y=0)

    def changehead(self):
        file_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg;*.png")])
        if file_path!= "":
            img = Instrument_img(file_path)
            img.resize_img(40,40)
            mage = img.get_img()
            global img_t
            img_t =ImageTk.PhotoImage(mage)
            self.btn1.config(image=img_t)
            
            with open(file_path, 'rb') as f:
                self.imgbyte = f.read()
  
    def clicked(self):  
        #注册按钮，往表中添加新用户
        if self.txt_user.get() == "" or self.txt_password.get() == "":
            messagebox.showinfo("提示", "请填写完整信息")
        else:
            user = self.txt_user.get()
            password = self.txt_password.get()
            head = self.imgbyte
            dic = {
                'nickname':user,
                'password':password,
                'user_desc':" ",
                'head':head
            }
            mid = Middleware()
            temp = mid.contian(user)
            if temp:
                messagebox.showinfo("提示", "该用户已存在")
            else:
                confi = mid.get_config()
                result = Result(**confi)
                result.start()
                result.insert(**dic)
                messagebox.showinfo("提示", "注册成功")
                result.close()
                


    def start(self):  
        self.master.mainloop()

# if __name__ == "__main__":  
#     root = Tk()  
#     app = EnrollWindow(root)  
#     app.start()
